﻿using GameDevClicker.Data;
using UnityEngine;

public class Game {
    private Resource resource { get { return Resource.Find(ResourceType.Money); } }
    private int modifier { get; set; }
    
    /// <summary>
    /// Returns the GameLevelData of the current available game (if none is available, returns the base game).
    /// </summary>
    public GameLevelData Current {
        get {
            int level = GetLevel();
            return StaticGameData.GameLevels[level];
        }
    }

    /// <summary>
    /// Returns the GameLevelData of the next available game (if none is available, return null).
    /// </summary>
    public GameLevelData Next {
        get {
            int level = GetLevel() + 1;
            if (level == StaticGameData.GameLevels.Count) {
                return null;
            }
            return StaticGameData.GameLevels[level];
        }
    }

    /// <summary>
    /// Gather income based on the current resources.
    /// </summary>
    public GameLevelData Sell(bool useModifier = true) {
        GameLevelData current = Current;
        int amount = current.Income;         
        if (useModifier) {
            if (useModifier) {
                float baseVal = (float)(amount) / 100f;
                float addition = baseVal * modifier;
                amount += (int)(addition);
            }
        }

        GameData.Instance.Code.Remove(current.CostCode);
        GameData.Instance.Art.Remove(current.CostArt);
        resource.Add(amount);

        return current;
    }

    /// <summary>
    /// Get the level of the highest available game to sell.
    /// </summary>
    /// <returns></returns>
    private int GetLevel() {
        int code = GameData.Instance.Code.Value;
        int art = GameData.Instance.Art.Value;

        int i = StaticGameData.GameLevels.Count;
        while (--i > -1) {
            GameLevelData gameData = StaticGameData.GameLevels[i];
            if (gameData.CostCode <= code && gameData.CostArt <= art) {
                return i;
            }
        }

        return 0;
    }
}
